package com.lsy.config;


import com.lsy.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;

import javax.sql.DataSource;

/**
 * 项目名：springboot01
 * 包名：com.lsy.config
 * 类名：SecurityConfig
 * 描述：
 * 作者：柳胜酉
 * 开始时间：2023/6/12
 * 完成时间：
 * 版本：1.0
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/index").permitAll()
                .antMatchers("/user/register").permitAll()
                .antMatchers("/user/doRegister").permitAll()
                .antMatchers("/votepage").permitAll()
                .antMatchers("/getLatestPK").permitAll()
                .antMatchers("/getVotes").permitAll()
                .antMatchers("/screen").permitAll()
                .antMatchers("/singer").hasAnyRole("3", "0")
                .antMatchers("/votepagejudge").hasAnyRole("2", "0")
                .antMatchers("/group").hasRole("0")
                .antMatchers("/votescountlist").hasRole("0")
                .antMatchers("/source/**").permitAll()
                .anyRequest().authenticated();
        http.formLogin()
                .loginPage("/user/login").permitAll()
                .loginProcessingUrl("/user/doLogin")
                .usernameParameter("name")
                .passwordParameter("password")
                .defaultSuccessUrl("/");
        //关闭CSRF防护功能
        http.csrf().disable();
        //开启用户退出功能
        http.logout()
                //配置用户退出处理控制URL
                //如果开启CSRF防护功能，则退出时使用POST请求
                .logoutUrl("/user/logout")
                //处理退出成功后的重定向地址
                .logoutSuccessUrl("/");

        //开启记住我功能
        http.rememberMe().rememberMeParameter("rememberMe").tokenValiditySeconds(3600)
                .tokenRepository(tokenRepository());

        http.exceptionHandling().accessDeniedPage("/noauth");
    }

    @Bean
    public JdbcTokenRepositoryImpl tokenRepository() {
        JdbcTokenRepositoryImpl jr = new JdbcTokenRepositoryImpl();
        jr.setDataSource(dataSource);
        return jr;
    }
}
